// pages/courses/coursesxiangqin/classAnswer/classAnswer.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url:'',
    questionId:0,
    answers:'',
    question:'',
    inputValue:'',
    userId:0,
  },
  getAnswer(){
    wx.request({
      url: this.data.url+'classAnswer/question/'+this.data.questionId,
      success:(resp)=>{
        console.log(resp.data.data);
        this.setData({
          answers:resp.data.data
        })
      },
      complete:()=>{
        wx.stopPullDownRefresh()
      }
    })
    
  },
  getQuestion(){
    wx.request({
      url: this.data.url+'classQuestion/getById/'+this.data.questionId,
      success:(resp)=>{
        console.log(resp.data);
        this.setData({
          question:resp.data.data
        })
      }
    })
  },
  /* 输入监听 */
  onInput(e){
    this.setData({ inputValue:e.detail.value });
  },
  /* 发送新评论 */
  onSend(){
    if (this.data.userId==0||this.data.userId==null) {
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      return;
    }
     const now = new Date();
     const sendTime = now.toISOString();
    wx.request({
      url: this.data.url+'classAnswer/add',
      method:'POST',
      data:{
        userId:this.data.userId,
        questionId:this.data.questionId,
        answerContent:this.data.inputValue,
        time:sendTime
      },
      success:(resp)=>{
        if (resp.data.code!=200) {
          wx.showToast({
            title: '发送失败',
            icon:"error"
          })
          return;
        }
        wx.showToast({
          title: '回复成功',
        })
        this.setData({
          inputValue:'',
        })
      },
    })
    console.log(this.data.inputValue)
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      questionId:options.id
    })
    this.setData({
      url:app.globalData.url
    })
   
    

    const user=wx.getStorageSync('user')
    this.setData({
      userId:user.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAnswer()
    this.getQuestion()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getAnswer()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})